﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Fifteen
{
    public partial class FifteenForm : Form
    {
        public int Columns = 4;
        public int Rows = 4;
        public const int EmptyIndex = 0;
        public const int MovableIndex = 1;
        public const int NotMovableIndex = 2;

        private TLayout layout;
        private int moves = 0;

        public FifteenForm(TLayout layout)
        {
            InitializeComponent();

            this.layout = layout;

            // Make ClientRectangle square
            int difference = this.ClientRectangle.Width - this.ClientRectangle.Height;
            this.Size = new Size(this.Size.Width, this.Size.Height + difference);

            this.Columns = this.layout.Columns;
            this.Rows = this.layout.Rows;

            Common.LayoutHelper.CreateControlsToFillClientArea(this, new Size(this.Columns, this.Rows), this.CreateButton);

            this.Controls[FifteenForm.EmptyIndex].Enabled = false;
        }

        public TButton CreateButton(int x, int y)
        {
            return new TButton(new Point(x, y), this.layout[x, y], this.layout.GetImageForFinalPosition(x, y));
        }

        public bool IsGameFinished()
        {
            foreach (Control control in this.Controls)
            {
                if (!GetButton(control).HasReachedFinalPosition)
                {
                    return false;
                }
            }

            return true;
        }

        public bool HasEmptyNeighbour(Point position)
        {
            TButton button = this.GetButton(this.Controls[FifteenForm.EmptyIndex]);
            
            if (((position.Y - button.Position.Y) == 0) && (Math.Abs(position.X - button.Position.X) == 1))
            {
                return true;
            }

            if (((position.X - button.Position.X) == 0) && (Math.Abs(position.Y - button.Position.Y) == 1))
            {
                return true;
            }

            return false;
        }

        public void MoveToEmpty(TButton button)
        {
            TButton emptyButton = this.GetButton(this.Controls[FifteenForm.EmptyIndex]);

            Point position = button.Position;
            button.Position = emptyButton.Position;
            emptyButton.Position = position;

            Point location = button.Location;
            button.Location = emptyButton.Location;
            emptyButton.Location = location;

            this.moves++;
            this.Text = "Fifteen (" + this.moves.ToString() + " moves)";

            if (this.IsGameFinished())
            {
                this.Controls.Clear();

                Button endGame = new Button();
                endGame.Location = new Point(0, 0);
                endGame.Size = this.ClientRectangle.Size;
                endGame.Image = this.layout.GetOriginalImage();
                endGame.Click += new EventHandler(endGame_Click);
                endGame.Text = "Game Finished. " + this.moves.ToString() + " moves. Click to Exit.";
                this.Controls.Add(endGame);
            }
        }

        void endGame_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        TButton GetButton(Control control)
        {
            return control as TButton;
        }
    }
}
